from nodetypes.ntypes.Document.showBase.MML import Document, Transform


class Document(Document):
    
    containsKey = 'mTranslations'
    groupingKey = u'translations'
    
    def filterContainedNodes(self, context, nodes, model, locale):                     
        model = self.getModelByMode(context, model)
                
        action = self.containsAction
        acls = context.acls.checkNodeAcls
        n_extend = nodes.extend
        
        # MAX DISPLAY LIMIT
        # if len(nodes) == max_display_limit:
        #   return
        # Should this be a user preference???
        
        # filter direct children nodes
        key = self.containsKey        
        n_extend(
            n for n in getattr(model,key) \
            if acls(n.rdf_getDomain(),action)
        )
                
        # if model IS A translation of another model,
        # then filter the other models matching translated nodes
        # of the children nodes
        curId = model.rdf_value
        for n in model.mTranslation_Of:
            n_extend(
                n for n in n.mTranslation \
                if n.rdf_value != curId and locale in n.dcLanguage and \
                    acls(n.rdf_domain, action)
            )
        
        # if model HAS translated models,
        # then filter the other models matching translated nodes
        # of the children nodes                     
        #for t in model.mTranslation:
        #    for c in getattr(t,key):
        #        n_extend(
        #            n for n in c.mTranslation \
        #            if locale in n.dcLanguage and \
        #                acls(n.rdf_domain,action)
        #        )

        return nodes    


class Transform(Transform):
    
    newDocument = Document